package com.zxy.supplier_system.server.repository

import com.zxy.supplier_system.server.controller.ProcurementSupplierController
import com.zxy.supplier_system.server.entity.ProcurementSupplier
import com.zxy.supplier_system.server.entity.SystemService
import com.zxy.supplier_system.server.utils.SpecificationUtils
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor

interface ProcurementSupplierRepository : JpaRepository<ProcurementSupplier, Int>, JpaSpecificationExecutor<ProcurementSupplier> {

    fun findAllBySystemService(systemService: SystemService): List<ProcurementSupplier>

    fun findByIdAndSystemService(id: Int, systemService: SystemService): ProcurementSupplier?

    object Specifications {

        fun fromQueryProcurementSupplier(
            queryProcurementSuppliersRequest: ProcurementSupplierController.QueryProcurementSuppliersRequest
        ): Specification<ProcurementSupplier> {
            return Specification.allOf(nameContains(queryProcurementSuppliersRequest.name))
        }

        fun nameContains(name: String?): Specification<ProcurementSupplier> {
            return SpecificationUtils.contains({
                it.get("name")
            }, name)
        }

    }

}
